package com.member.web.job;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.enums.ReceiveStatusEnum;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizCouponReceive;
import com.member.shop.mapper.SysBizCouponReceiveMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务(优惠券过期截至)
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-11-30
 **/
@Component
@Log4j2
public class CouponJob {

    @Resource
    private SysBizCouponReceiveMapper sysBizCouponReceiveMapper;

    /**
     * 从1日开始,每1天执行一次
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void sync() {
        //转换为字符串
        LambdaQueryWrapper<SysBizCouponReceive> sysBizCouponReceiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizCouponReceiveLambdaQueryWrapper.eq(SysBizCouponReceive::getStatus, ReceiveStatusEnum.UNUSED.getKey());
        List<SysBizCouponReceive> sysBizCouponReceives = sysBizCouponReceiveMapper.selectList(sysBizCouponReceiveLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(sysBizCouponReceives)) {
            for (SysBizCouponReceive sysBizCouponReceive : sysBizCouponReceives) {
                if (DateUtils.getNowDate().compareTo(sysBizCouponReceive.getTermEndTime()) > 0) {
                    sysBizCouponReceive.setStatus(ReceiveStatusEnum.EXPIRED.getKey());
                    sysBizCouponReceiveMapper.updateById(sysBizCouponReceive);
                }
            }
        }
    }

}
